﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Exdot
{
    public partial class DoThi : Form
    {
        public DoThi()
        {
            InitializeComponent();
        }

        private void DoThi_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            chart1.Visible = true;
            int max = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
            for (int i = 1; i < 12; i++)
                if (max < Convert.ToInt32(dataGridView1.CurrentRow.Cells[i].Value))
                    max = Convert.ToInt32(dataGridView1.CurrentRow.Cells[i].Value);
            if (chart1.ChartAreas[0].AxisY.Maximum < max) chart1.ChartAreas[0].AxisY.Maximum = max;

            chart1.Series.Clear();
            for (int n = 0; n < dataGridView1.Rows.Count; n++) //Duyệt từ dòng đầu tiên đến dòng cuối cùng của dataGridView1
            {
                if (dataGridView1.Rows[n].Selected) //Nếu dòng thứ n được chọn thì thêm series cho dòng đó
                {
                    Series s = new Series();
                    s.Name = dataGridView1.Rows[n].HeaderCell.Value.ToString();
                    for (int i = 0; i < 12; i++)
                    {
                        DataPoint p = new DataPoint();
                        p.XValue = i;
                        p.SetValueY(Convert.ToDouble(dataGridView1.Rows[n].Cells[i].Value));
                        p.AxisLabel = "Tháng " + (i + 1).ToString();
                        s.Points.Add(p);
                    }
                    chart1.Series.Add(s);
                }
            }
        }
        private double GetCTThang(int thang, int nam)
        {
            QuanLyChiTieuEntities ql = new QuanLyChiTieuEntities();
            double giatri = 0;

            var query = from ct in ql.ChiTieux
                        where ct.ThoiGian.Month.Equals(thang) && ct.ThoiGian.Year.Equals(nam)
                        select ct;
            if (query.Any())
            {
                for (int i = 0; i < query.ToList().Count; i++)
                    giatri += query.ToList()[i].SoTien;
            }

            return giatri;
        }
        private double GetTNThang(int thang, int nam)
        {
            QuanLyChiTieuEntities ql = new QuanLyChiTieuEntities();
            double giatri = 0;

            var query = from ct in ql.ThuNhaps
                        where ct.ThoiGian.Month.Equals(thang) && ct.ThoiGian.Year.Equals(nam)
                        select ct;
            if (query.Any())
            {
                for (int i = 0; i < query.ToList().Count; i++)
                    giatri += query.ToList()[i].SoTien;
            }

            return giatri;
        }
        private void DoThi_Load_1(object sender, EventArgs e)
        {
            int nam = int.Parse(DateTime.Now.Year.ToString());
            txtNam.Text = nam.ToString();
            loadG(nam);
        }
        private void loadG(int nam)
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[0].HeaderCell.Value = "Chi Tiêu";
            dataGridView1.Rows[0].Cells[0].Value = GetCTThang(1, nam).ToString();
            dataGridView1.Rows[0].Cells[1].Value = GetCTThang(2, nam).ToString();
            dataGridView1.Rows[0].Cells[2].Value = GetCTThang(3, nam).ToString();
            dataGridView1.Rows[0].Cells[3].Value = GetCTThang(4, nam).ToString();
            dataGridView1.Rows[0].Cells[4].Value = GetCTThang(5, nam).ToString();
            dataGridView1.Rows[0].Cells[5].Value = GetCTThang(6, nam).ToString();
            dataGridView1.Rows[0].Cells[6].Value = GetCTThang(7, nam).ToString();
            dataGridView1.Rows[0].Cells[7].Value = GetCTThang(8, nam).ToString();
            dataGridView1.Rows[0].Cells[8].Value = GetCTThang(9, nam).ToString() ;
            dataGridView1.Rows[0].Cells[9].Value = GetCTThang(10, nam).ToString();
            dataGridView1.Rows[0].Cells[10].Value = GetCTThang(11, nam).ToString();
            dataGridView1.Rows[0].Cells[11].Value = GetCTThang(12, nam).ToString();
            // du lieu cho thu nhap
            dataGridView1.Rows.Add();
            dataGridView1.Rows[1].HeaderCell.Value = "Thu Nhập";
            dataGridView1.Rows[1].Cells[0].Value = GetTNThang(1,nam).ToString();
            dataGridView1.Rows[1].Cells[1].Value = GetTNThang(2, nam).ToString();
            dataGridView1.Rows[1].Cells[2].Value = GetTNThang(3, nam).ToString();
            dataGridView1.Rows[1].Cells[3].Value = GetTNThang(4, nam).ToString();
            dataGridView1.Rows[1].Cells[4].Value = GetTNThang(5, nam).ToString();
            dataGridView1.Rows[1].Cells[5].Value = GetTNThang(6, nam).ToString();
            dataGridView1.Rows[1].Cells[6].Value = GetTNThang(7, nam).ToString();
            dataGridView1.Rows[1].Cells[7].Value = GetTNThang(8, nam).ToString();
            dataGridView1.Rows[1].Cells[8].Value = GetTNThang(9, nam).ToString();
            dataGridView1.Rows[1].Cells[9].Value = GetTNThang(10, nam).ToString();
            dataGridView1.Rows[1].Cells[10].Value = GetTNThang(11, nam).ToString();
            dataGridView1.Rows[1].Cells[11].Value = GetTNThang(12, nam).ToString();

        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            if (txtNam.Text != "")
            {
                loadG(int.Parse(txtNam.Text));
            }
        }

        

        

    }
}
